package com.bdrm.service.impl;

import com.bdrm.constant.MessageConstant;
import com.bdrm.constant.StatusConstant;
import com.bdrm.dto.DishDTO;
import com.bdrm.dto.DishPageQueryDTO;
import com.bdrm.entity.Dish;
import com.bdrm.entity.DishFlavor;
import com.bdrm.exception.DeletionNotAllowedException;
import com.bdrm.mapper.DishFlavorMapper;
import com.bdrm.mapper.DishMapper;
import com.bdrm.mapper.SetmealDishMapper;
import com.bdrm.result.PageResult;
import com.bdrm.service.DishService;
import com.bdrm.vo.DishVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<Dish> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insert(dish);

        //插入返回的id
        Long id = dish.getId();

        //先查看口味数组里面是否有数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavors -> {
                dishFlavors.setDishId(id);
            });
            System.out.println("口味"+flavors);
            // 向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public DishVO getDishById(Long id) {

        // 根据id查询菜品
        Dish dish = dishMapper.getDishById(id);

        // 根据菜品id查询口味
        List<DishFlavor>dishFlavors = dishFlavorMapper.getByDishId(id);

        //转换数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //修改菜品信息
        dishMapper.update(dish);

        //修改口味信息(先删除再添加)
        // 删除口味信息
        dishFlavorMapper.deleteByDishId(dish.getId());

        //添加口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //  向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Transactional
    @Override
    public void deleteById(List<Long> ids) {

        // 1.启用的菜品不能删除
        for (Long id : ids){
            Dish dish = dishMapper.getDishById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 2.套餐关联的菜品不能删除
        List<Long> setmealIds =setmealDishMapper.getSetmealByDishId(ids);
        if (setmealIds != null && setmealIds.size() > 0){
            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.deleteByIds(ids);
        //再删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish build = Dish.builder()
                .status(status)
                .id(id)
                .build();

        dishMapper.update(build);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
